//package com.tianyuan.lims.dms.service;
//
//import com.jfinal.aop.Inject;
//import com.tianyuan.lims.dms.entity.*;
//import com.tianyuan.lims.erp.admin.service.AdminUserService;
//
//import java.time.LocalDate;
//import java.time.LocalDateTime;
//import java.time.ZoneId;
//import java.util.*;
//import java.util.concurrent.atomic.AtomicReference;
//import java.util.stream.Collectors;
//
//public class YwfpService {
//    @Inject
//    private AdminUserService adminUserService;
//
//    @Inject
//    private LimsDataSourceService dataSourceService;
//    @Inject
//    private CityService cityService;
//    @Inject
//    private ProvinceLaboratoryService provinceLaboratoryService;
//
//
//    public Map<String, String> getTaskNumByAreaStat(String deptId, String deptTypeId) {
//        Map<String, String> taskNumMap = new HashMap<>();
//        taskNumMap.put("provinceCount", "");
//        taskNumMap.put("cityCount", "");
//        taskNumMap.put("districtCount", "");
//        taskNumMap.put("otherCount", "");
//        taskNumMap.put("totalCount", "");
//        taskNumMap.put("todayCount", "");
//        try {
//
//            List<LimsArea> allAreas = LimsArea.dao.find("SELECT * FROM lims_area");
//            Integer orgtypeid = 0;
//            Integer orgid;
//            LimsOrganization limsOrganization = LimsOrganization.dao.findFirst("select * from lims_organization where orgtypeid = " + deptTypeId + " and deptid = " + deptId);
//            if (limsOrganization != null) {
//                orgtypeid = limsOrganization.getOrgtypeid();
//                orgid = limsOrganization.getDeptid();
//            } else {
//                orgid = 0;
//            }
//            List<LimsUpYwfp> upYwfpList = LimsUpYwfp.dao.find("select * from lims_up_ywfp where flag = 1 ");
//
//            List<LimsArea> provinceList = new ArrayList<>();
//            List<LimsArea> cityList = new ArrayList<>();
//            List<LimsArea> districtList = new ArrayList<>();
//
//            if (orgtypeid == 1) {
//                provinceList = allAreas.stream().filter(limsArea -> limsArea.getId() != null && limsArea.getId().intValue() == orgid).collect(Collectors.toList());
//                cityList = allAreas.stream().filter(limsArea -> limsArea.getPid() != null && limsArea.getPid().intValue() == orgid).collect(Collectors.toList());
//                // districtList 使用cityList 的id 进行过滤
//                List<Integer> cityIds = cityList.stream()
//                        .map(LimsArea::getId)
//                        .filter(Objects::nonNull)
//                        .collect(Collectors.toList());
//
//                districtList = allAreas.stream()
//                        .filter(area -> area != null && area.getPid() != null && cityIds.contains(area.getPid()))
//                        .collect(Collectors.toList());
//            } else if (orgtypeid == 2) {
//                cityList = allAreas.stream().filter(limsArea -> limsArea.getId() != null && limsArea.getId().intValue() == orgid).collect(Collectors.toList());
//                // districtList 使用cityList 的id 进行过滤
//                List<Integer> cityIds = cityList.stream()
//                        .map(LimsArea::getId)
//                        .filter(Objects::nonNull)
//                        .collect(Collectors.toList());
//
//                districtList = allAreas.stream()
//                        .filter(area -> area != null && area.getPid() != null && cityIds.contains(area.getPid()))
//                        .collect(Collectors.toList());
//            } else if (orgtypeid == 3) {
//                districtList = allAreas.stream().filter(limsArea -> limsArea.getId() != null && limsArea.getId().intValue() == orgid).collect(Collectors.toList());
//            }
//
//
//            AtomicReference<Long> provinceCount = new AtomicReference<>(0L);
//            AtomicReference<Long> cityCount = new AtomicReference<>(0L);
//            AtomicReference<Long> districtCount = new AtomicReference<>(0L);
//            AtomicReference<Long> otherCount = new AtomicReference<>(0L);
//
//            provinceList.forEach(limsArea -> {
//                Integer orgTypeId = limsArea.getLevel();
//                Integer orgId = limsArea.getId();
//
//                long count = upYwfpList.stream().filter(limsUpYwfp -> limsUpYwfp.getOrgTypeId() != null
//                        && limsUpYwfp.getOrgTypeId().intValue() == orgTypeId.intValue()
//                        && limsUpYwfp.getOrgId() != null
//                        && limsUpYwfp.getOrgId().intValue() == orgId.intValue()).count();
//                provinceCount.set(provinceCount.get() + count);
//            });
//
//
//            cityList.forEach(limsArea -> {
//                Integer orgTypeId = limsArea.getLevel();
//                Integer orgId = limsArea.getId();
//
//                long count = upYwfpList.stream().filter(limsUpYwfp -> limsUpYwfp.getOrgTypeId() != null
//                        && limsUpYwfp.getOrgTypeId().intValue() == orgTypeId.intValue()
//                        && limsUpYwfp.getOrgId() != null
//                        && limsUpYwfp.getOrgId().intValue() == orgId.intValue()).count();
//                cityCount.set(cityCount.get() + count);
//            });
//
//
//            districtList.forEach(limsArea -> {
//                Integer orgTypeId = limsArea.getLevel();
//                Integer orgId = limsArea.getId();
//
//                long count = upYwfpList.stream().filter(limsUpYwfp -> limsUpYwfp.getOrgTypeId() != null
//                        && limsUpYwfp.getOrgTypeId().intValue() == orgTypeId.intValue()
//                        && limsUpYwfp.getOrgId() != null
//                        && limsUpYwfp.getOrgId().intValue() == orgId.intValue()).count();
//                districtCount.set(districtCount.get() + count);
//            });
//
//            List<LimsOrganization> otherList = new ArrayList<>();
//
//            if (orgtypeid == 1) {
//                otherList = LimsOrganization.dao.find("select * from lims_organization where orgtypeid = 4 and province = " + deptId);
//            } else if (orgtypeid == 2) {
//                otherList = LimsOrganization.dao.find("select * from lims_organization where orgtypeid = 4 and city = " + deptId);
//            } else if (orgtypeid == 3) {
//                otherList = LimsOrganization.dao.find("select * from lims_organization where orgtypeid = 4 and district = " + deptId);
//            } else if (orgtypeid == 4) {
//                otherList = LimsOrganization.dao.find("select * from lims_organization where orgtypeid = 4 and deptid = " + deptId);
//            }
//            otherList.forEach(limsArea -> {
//                Integer orgTypeId = limsArea.getOrgtypeid();
//                Long orgId = limsArea.getId();
//
//                long count = upYwfpList.stream().filter(limsUpYwfp -> limsUpYwfp.getOrgTypeId() != null
//                        && limsUpYwfp.getOrgTypeId().intValue() == orgTypeId.intValue()
//                        && limsUpYwfp.getOrgId() != null
//                        && limsUpYwfp.getOrgId() == orgId.intValue()).count();
//                otherCount.set(otherCount.get() + count);
//            });
//
//            taskNumMap.put("provinceCount", provinceCount.get() + "");
//            taskNumMap.put("cityCount", cityCount.get() + "");
//            taskNumMap.put("districtCount", districtCount.get() + "");
//            taskNumMap.put("otherCount", otherCount.get() + "");
//            taskNumMap.put("totalCount", provinceCount.get() + cityCount.get() + districtCount.get() + otherCount.get() + "");
//
//
//            // upYwfpList Date类型  过滤今天00:00 - 23:59
//            // 获取当天起始时间（系统默认时区）
//            LocalDate today = LocalDate.now();
//            LocalDateTime startOfDay = today.atStartOfDay();
//            LocalDateTime endOfDay = today.plusDays(1).atStartOfDay().minusNanos(1);
//
//            // 转换为Date对象（根据项目需要选择时区）
//            Date startDate = Date.from(startOfDay.atZone(ZoneId.systemDefault()).toInstant());
//            Date endDate = Date.from(endOfDay.atZone(ZoneId.systemDefault()).toInstant());
//
//            // 过滤逻辑
//            List<LimsUpYwfp> filteredList = upYwfpList.stream()
//                    .filter(limsUpYwfp -> {
//                        Date sampleTime = limsUpYwfp.getBeginTime();
//                        if (sampleTime == null) return false;
//
//                        // 使用!before和!after来实现包含边界的时间判断
//                        return !sampleTime.before(startDate) && !sampleTime.after(endDate);
//                    })
//                    .collect(Collectors.toList());
//            taskNumMap.put("todayCount", filteredList.size() + "");
//        } catch (Exception e) {
//
//        }
//        return taskNumMap;
//    }
//
//
//    public Map<String, String> getOrgNumByAreaStat(String deptId, String deptTypeId) {
//        Map<String, String> resultMap = new HashMap<>();
//        resultMap.put("provinceCount", "");
//        resultMap.put("cityCount", "");
//        resultMap.put("districtCount", "");
//        resultMap.put("otherCount", "");
//        resultMap.put("totalCount", "");
//        try {
//
//
//            List<LimsArea> allAreas = LimsArea.dao.find("SELECT * FROM lims_area");
//            Integer orgtypeid = 0;
//            Integer orgid;
//            List<LimsOrganization> allOrgList = LimsOrganization.dao.find("select * from lims_organization ");
//            LimsOrganization limsOrganization = LimsOrganization.dao.findFirst("select * from lims_organization where orgtypeid = " + deptTypeId + " and  deptid = " + deptId);
//            if (limsOrganization != null) {
//                orgtypeid = limsOrganization.getOrgtypeid();
//                orgid = limsOrganization.getDeptid();
//            } else {
//                orgid = 0;
//            }
//
//            List<LimsArea> provinceList = new ArrayList<>();
//            List<LimsArea> cityList = new ArrayList<>();
//            List<LimsArea> districtList = new ArrayList<>();
//
//            if (orgtypeid == 1) {
//                provinceList = allAreas.stream().filter(limsArea -> limsArea.getId() != null && limsArea.getId().intValue() == orgid).collect(Collectors.toList());
//                cityList = allAreas.stream().filter(limsArea -> limsArea.getPid() != null && limsArea.getPid().intValue() == orgid).collect(Collectors.toList());
//                // districtList 使用cityList 的id 进行过滤
//                List<Integer> cityIds = cityList.stream()
//                        .map(LimsArea::getId)
//                        .filter(Objects::nonNull)
//                        .collect(Collectors.toList());
//
//                districtList = allAreas.stream()
//                        .filter(area -> area != null && area.getPid() != null && cityIds.contains(area.getPid()))
//                        .collect(Collectors.toList());
//            } else if (orgtypeid == 2) {
//                cityList = allAreas.stream().filter(limsArea -> limsArea.getId() != null && limsArea.getId().intValue() == orgid).collect(Collectors.toList());
//                // districtList 使用cityList 的id 进行过滤
//                List<Integer> cityIds = cityList.stream()
//                        .map(LimsArea::getId)
//                        .filter(Objects::nonNull)
//                        .collect(Collectors.toList());
//
//                districtList = allAreas.stream()
//                        .filter(area -> area != null && area.getPid() != null && cityIds.contains(area.getPid()))
//                        .collect(Collectors.toList());
//            } else if (orgtypeid == 3) {
//                districtList = allAreas.stream().filter(limsArea -> limsArea.getId() != null && limsArea.getId().intValue() == orgid).collect(Collectors.toList());
//            }
//
//
//            AtomicReference<Long> provinceCount = new AtomicReference<>(0L);
//            AtomicReference<Long> cityCount = new AtomicReference<>(0L);
//            AtomicReference<Long> districtCount = new AtomicReference<>(0L);
//            AtomicReference<Long> otherCount = new AtomicReference<>(0L);
//
//            provinceList.forEach(limsArea -> {
//                Integer orgTypeId = limsArea.getLevel();
//                Integer orgId = limsArea.getId();
//
//                long count = allOrgList.stream().filter(org -> org.getOrgtypeid() != null
//                        && org.getOrgtypeid().intValue() == orgTypeId.intValue()
//                        && org.getDeptid() != null
//                        && org.getDeptid().intValue() == orgId.intValue()).count();
//                provinceCount.set(provinceCount.get() + count);
//            });
//
//            cityList.forEach(limsArea -> {
//                Integer orgTypeId = limsArea.getLevel();
//                Integer orgId = limsArea.getId();
//
//                long count = allOrgList.stream().filter(org -> org.getOrgtypeid() != null
//                        && org.getOrgtypeid().intValue() == orgTypeId.intValue()
//                        && org.getDeptid() != null
//                        && org.getDeptid().intValue() == orgId.intValue()).count();
//                cityCount.set(cityCount.get() + count);
//            });
//            districtList.forEach(limsArea -> {
//                Integer orgTypeId = limsArea.getLevel();
//                Integer orgId = limsArea.getId();
//
//                long count = allOrgList.stream().filter(org -> org.getOrgtypeid() != null
//                        && org.getOrgtypeid().intValue() == orgTypeId.intValue()
//                        && org.getDeptid() != null
//                        && org.getDeptid().intValue() == orgId.intValue()).count();
//                districtCount.set(districtCount.get() + count);
//            });
//
//            List<LimsOrganization> otherList = new ArrayList<>();
//
//            if (orgtypeid == 1) {
//                otherList = LimsOrganization.dao.find("select * from lims_organization where orgtypeid = 4 and  province = " + deptId);
//            } else if (orgtypeid == 2) {
//                otherList = LimsOrganization.dao.find("select * from lims_organization where orgtypeid = 4 and city = " + deptId);
//            } else if (orgtypeid == 3) {
//                otherList = LimsOrganization.dao.find("select * from lims_organization where orgtypeid = 4 and district = " + deptId);
//            } else if (orgtypeid == 4) {
//                otherList = LimsOrganization.dao.find("select * from lims_organization where orgtypeid = 4 and deptid = " + deptId);
//            }
//            otherCount.set((long) otherList.size());
//
//            resultMap.put("provinceCount", provinceCount.get() + "");
//            resultMap.put("cityCount", cityCount.get() + "");
//            resultMap.put("districtCount", districtCount.get() + "");
//            //resultMap.put("otherCount", otherCount.get() + "");
//            resultMap.put("otherCount", "0");
//            resultMap.put("totalCount", provinceCount.get() + cityCount.get() + districtCount.get() + otherCount.get() + "");
//
//        } catch (Exception e) {
//
//        }
//        return resultMap;
//    }
//}
